package com.luo.chengrui.module.sa.controller.admin.org;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.org.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import com.luo.chengrui.module.sa.convert.org.OrgConvert;
import com.luo.chengrui.module.sa.service.org.OrgService;

@Tag(name = "管理后台 - 组织机构管理")
@RestController
@RequestMapping("/sa/org")
@Validated
public class OrgController {

    @Resource
    private OrgService orgService;

    @PostMapping("/create")
    @Operation(summary = "创建组织机构管理")
    @PreAuthorize("@ss.hasPermission('sa:org:create')")
    public CommonResult<String> createOrg(@Valid @RequestBody OrgCreateReqVO createReqVO) {
        return success(orgService.createOrg(createReqVO));
    }

    @PostMapping("/batchCreatePsm")
    @Operation(summary = "创建组织机构管理")
    @PreAuthorize("@ss.hasPermission('sa:org:create')")
    public CommonResult<Boolean> batchCreatePsm(@Valid @RequestBody List<OrgCreateReqVO> createReqVO) {
        orgService.batchCreatePsm(createReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新组织机构管理")
    @PreAuthorize("@ss.hasPermission('sa:org:update')")
    public CommonResult<Boolean> updateOrg(@Valid @RequestBody OrgUpdateReqVO updateReqVO) {
        orgService.updateOrg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织机构管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:org:delete')")
    public CommonResult<Boolean> deleteOrg(@RequestParam("id") String id) {
        orgService.deleteOrg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织机构管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:org:query')")
    public CommonResult<OrgRespVO> getOrg(@RequestParam("id") String id) {
        OrgDO org = orgService.getOrg(id);
        return success(OrgConvert.INSTANCE.convert(org));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织机构管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:org:query')")
    public CommonResult<List<OrgRespVO>> getOrgList(@RequestParam("ids") Collection<String> ids) {
        List<OrgDO> list = orgService.getOrgList(ids);
        return success(OrgConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织机构管理分页")
    @PreAuthorize("@ss.hasPermission('sa:org:query')")
    public CommonResult<PageResult<OrgRespVO>> getOrgPage(@Valid OrgPageReqVO pageVO) {
        PageResult<OrgDO> pageResult = orgService.getOrgPage(pageVO);
        return success(OrgConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织机构管理 Excel")
    @PreAuthorize("@ss.hasPermission('sa:org:export')")
    @OperateLog(type = EXPORT)
    public void exportOrgExcel(@Valid OrgExportReqVO exportReqVO,
                               HttpServletResponse response) throws IOException {
        List<OrgDO> list = orgService.getOrgList(exportReqVO);
        // 导出 Excel
        List<OrgExcelVO> datas = OrgConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织机构管理.xls", "数据", OrgExcelVO.class, datas);
    }

    @GetMapping("/tree")
    @Operation(summary = "获得组织机构管理")
    @Parameter(name = "id", description = "id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:org:query')")
    public CommonResult<List<OrgRespVO>> getOrgTree(@RequestParam("id") String id) {
        List<OrgDO> list = orgService.getChildrenBy(id);
        return success(OrgConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/sync")
    @Operation(summary = "同步机构")
    @Parameter(name = "id", description = "id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:org:create')")
    public CommonResult<Boolean> syncOrg(@RequestParam("id") String id) {
        orgService.syncOrg(id);
        return success(true);
    }

}
